package tju.mic.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
private static final DateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static void TestConflication(){
        System.out.println("我是程序员1");
    }

    public static Date addMonths(Date d, int months) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.add(Calendar.MONTH, months);
        return c.getTime();
    }
    
    public static Date addDays(Date d, int days) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.add(Calendar.DATE, days);
        return c.getTime();
    }

    public static Date addMinutes(Date d, int minutes) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.add(Calendar.MINUTE, minutes);
        return c.getTime();
    }    
    
    public static long daysBetween(Date startDate, Date endDate) {
        return msBetween(startDate, endDate) / (1000 * 60 * 60 * 24);
    }

    
    public static Date firstOfMonth(Date d) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.set(Calendar.DATE, 1);
        return c.getTime();
    }

    public static long hoursBetween(Date startDate, Date endDate) {
        return msBetween(startDate, endDate) / (1000 * 60 * 60);
    }

    public static long minutesBetween(Date startDate, Date endDate) {
        return msBetween(startDate, endDate) / (1000 * 60);
    }

    public static long msBetween(Date startDate, Date endDate) {
        return endDate.getTime() - startDate.getTime();
    }

    public static long secondsBetween(Date startDate, Date endDate) {
        return msBetween(startDate, endDate) / 1000;
    }

    public static Date yesterday() {
        return addDays(today(), -1);
    }

    public static Date today() {
        return trunc(new Date());
    }

    public static Date now() {
        return new Date();
    }
    
    public static Date date(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, day, 0, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }
    
    public static Date date(int year, int month, int day, int hours, int mins, int secs) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, day, hours, mins, secs);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }    
    
    public static Date date(int year, int month, int day, int hours, int mins, int secs, int millis) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, day, hours, mins, secs);
        c.set(Calendar.MILLISECOND, millis);
        return c.getTime();
    }     

    public static Date trunc(Date d) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    public static Date parse(String date) throws ParseException {
        synchronized(DateUtils.class) {
            return DEFAULT_DATE_FORMAT.parse(date);
        }
    }

    public static Date parse(String date, String fmt) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(fmt);
        return df.parse(date);
    }

    public static String format(Date date) throws ParseException {
        synchronized(DateUtils.class) {
            return DEFAULT_DATE_FORMAT.format(date);
        }
    }

    public static String format(Date date, String fmt) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(fmt);
        return df.format(date);
    }
}
